package com.lagou.biz.client;

import com.lagou.niancheng.biz.api.SimpleServiceApi;
import com.lagou.niancheng.rpc.client.SimpleRpcMethodProxyFactory;
import com.lagou.niancheng.rpc.client.netty.LoadBalanceStrategy;
import com.lagou.niancheng.rpc.client.zookeeper.ZookeeperConnectionConfig;
import com.lagou.niancheng.rpc.client.zookeeper.ZookeeperConnectionSource;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

/**
 * BizClientForZookeeper
 *
 * @author wlz
 * @date 2020/5/27
 */
public class BizClientForZookeeper {


    private SimpleServiceApi serviceApi;

    @Before
    public void init() {
        // 创建zookeeper连接源配置
        ZookeeperConnectionConfig zookeeperConnectionConfig = new ZookeeperConnectionConfig();
        zookeeperConnectionConfig.setLoadBalanceStrategy(LoadBalanceStrategy.LONGEST_RESPONSE);
        zookeeperConnectionConfig.setZookeeperHost("localhost:2181");
        ZookeeperConnectionSource zookeeperConnectionSource = new ZookeeperConnectionSource(zookeeperConnectionConfig);
        // 创建代理工厂
        SimpleRpcMethodProxyFactory simpleRpcMethodProxyFactory = new SimpleRpcMethodProxyFactory(zookeeperConnectionSource);
        // userApi初始化
        serviceApi = simpleRpcMethodProxyFactory.createRpcProxy(SimpleServiceApi.class);
    }


    @Test
    public void clientTest() throws InterruptedException {
        while (true) {
            try {
                String data = serviceApi.getData();
                System.out.println(data);
            } catch (Throwable e) {
                System.err.println("执行出错:" + e.getMessage());
            }
            TimeUnit.SECONDS.sleep(1);
        }
    }

}
